#ifndef LSM303DLH_M_H_INCLUDED
#define LSM303DLH_M_H_INCLUDED

#define MAG_Mode_file "/sys/class/i2c-dev/i2c-2/device/2-001e/mode"
#define MAG_Name_file "/sys/bus/i2c/drivers/lsm303dlh_m/2-001e/name"
#define MAG_Rate_file "/sys/class/i2c-dev/i2c-2/device/2-001e/rate"
#define MAG_Gain_file "/sys/class/i2c-dev/i2c-2/device/2-001e/gain"
#define MAG_Data_file "/sys/class/i2c-dev/i2c-2/device/2-001e/data"
#define MAG_Range_file "/sys/class/i2c-dev/i2c-2/device/2-001e/range"

/* Magnetometer capturing mode  */
#define LSM303DLH_M_MODE_CONTINUOUS 0
#define LSM303DLH_M_MODE_SINGLE 1
#define LSM303DLH_M_MODE_SLEEP 3

/* Magnetometer range setting */
#define LSM303DLH_M_RANGE_1_3G  0x01
#define LSM303DLH_M_RANGE_1_9G  0x02
#define LSM303DLH_M_RANGE_2_5G  0x03
#define LSM303DLH_M_RANGE_4_0G  0x04
#define LSM303DLH_M_RANGE_4_7G  0x05
#define LSM303DLH_M_RANGE_5_6G  0x06
#define LSM303DLH_M_RANGE_8_1G  0x07

/* Magnetometer output data rate */
#define LSM303DLH_M_RATE_00_75 0x00
#define LSM303DLH_M_RATE_01_50 0x01
#define LSM303DLH_M_RATE_03_00 0x02
#define LSM303DLH_M_RATE_07_50 0x03
#define LSM303DLH_M_RATE_15_00 0x04
#define LSM303DLH_M_RATE_30_00 0x05
#define LSM303DLH_M_RATE_75_00 0x06

/* Magnetometer X-Y gain  */
#define LSM303DLH_M_XY_GAIN_1_3	1055 /* XY gain at 1.3G */
#define LSM303DLH_M_XY_GAIN_1_9	 795 /* XY gain at 1.9G */
#define LSM303DLH_M_XY_GAIN_2_5	 635 /* XY gain at 2.5G */
#define LSM303DLH_M_XY_GAIN_4_0	 430 /* XY gain at 4.0G */
#define LSM303DLH_M_XY_GAIN_4_7	 375 /* XY gain at 4.7G */
#define LSM303DLH_M_XY_GAIN_5_6	 320 /* XY gain at 5.6G */
#define LSM303DLH_M_XY_GAIN_8_1	 230 /* XY gain at 8.1G */

/* Magnetometer Z gain  */
#define LSM303DLH_M_Z_GAIN_1_3	950 /* Z gain at 1.3G */
#define LSM303DLH_M_Z_GAIN_1_9	710 /* Z gain at 1.9G */
#define LSM303DLH_M_Z_GAIN_2_5	570 /* Z gain at 2.5G */
#define LSM303DLH_M_Z_GAIN_4_0	385 /* Z gain at 4.0G */
#define LSM303DLH_M_Z_GAIN_4_7	335 /* Z gain at 4.7G */
#define LSM303DLH_M_Z_GAIN_5_6	285 /* Z gain at 5.6G */
#define LSM303DLH_M_Z_GAIN_8_1	205 /* Z gain at 8.1G */


typedef struct {
	char name[16];
	short updated;
	struct {
        int mode;
        int range;
        int rate;
        int gain[3];
    } config;
	struct {
		double axis[3];
	} raw;
	struct {
		double axis[3];
	} scaled;
	struct {
		double bias[3];
		double A_1[3*3];
		/*
		float variance[3];
		float scale[3];
		*/
	} calibration;
} mag_sensor;

void reset_values_mag(mag_sensor *mag);
int Mag_Init(mag_sensor *mag);
int Mag_write_config(mag_sensor *mag);
int Mag_read_config(mag_sensor *mag);
int Mag_read_data(mag_sensor *mag);

void scaled_data(mag_sensor *mag);

#endif // LSM303DLH_M_H_INCLUDED
